//
//  ReadViewController.m
//  Lucky28
//
//  Created by zr on 2018/7/31.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "ReadViewController.h"
#import "TableHeaderView.h"
#import "ReadTableViewCell.h"
#import "ReadHeadView.h"
#import "SearchViewController.h"
#import "ReadModel.h"
#import "ReadInfoViewController.h"
#import "ZSContentViewController.h"
#import "ZSContentNetworkTools.h"
#import "AppDelegate.h"

@interface ReadViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSArray<ReadModel *>* readListArray;


@end

@implementation ReadViewController


//- (void)viewWillAppear:(BOOL)animated {
//    [super viewWillAppear:animated];
//    
////    [self.navigationController setNavigationBarHidden:YES animated:animated];
//    
//    [UIApplication sharedApplication].statusBarHidden = YES;
//    
//    [super viewWillAppear:animated];
//}
//
//- (void)viewWillDisappear:(BOOL)animated {
//    [super viewWillDisappear:animated];
//    
//    [self.navigationController setNavigationBarHidden:NO animated:animated];
//    
//    [super viewWillDisappear:animated];
//}


- (void)setReadListArray:(NSArray<ReadModel *> *)readListArray{
    
    _readListArray = readListArray;
    
    [self.tableView reloadData];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [ReadModel readWithType:@"10" WithSucess:^(NSArray<ReadModel *> *array) {
        
        self.readListArray = array;
    } error:^{
        
        NSLog(@"productList error");
    }];
    
    [self setupNSNoti];
    [self setupUI];
//    [self goContentAction];
}

- (void)setupNSNoti{
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushToSearchVC) name:@"pushToSearchVC" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getReadData:) name:@"getReadData" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushToInfoVC:) name:@"pushToInfoVC" object:nil];
}

- (void)setupUI{
    
//    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT - 110)];
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT - 80) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.rowHeight = 270 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    TableHeaderView * headerView =  [[TableHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 450 * SCREENH6)];
    // 设置header
    tableView.tableHeaderView = headerView;
    //    self.tableView.bounces = NO;
    //    self.tableView.estimatedRowHeight = 150 * SCREENH6;//估算高度
    //    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[ReadTableViewCell class] forCellReuseIdentifier:@"ReadListCell"];
    [self.tableView registerClass:[ReadHeadView class] forHeaderFooterViewReuseIdentifier:@"HeaderView"];
    
    [self.view addSubview:self.tableView];
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.readListArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ReadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ReadListCell" forIndexPath:indexPath];

    cell.model = self.readListArray[indexPath.row];

    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 50* SCREENH6;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    ReadHeadView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"HeaderView"];
    
    return header;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ReadModel * model = self.readListArray[indexPath.row];
    
    ReadInfoViewController * infoVC = [ReadInfoViewController new];
    
    NSURL * url = [NSURL URLWithString:model.images];
    
    infoVC.url = url;
    infoVC.titleString = model.title;
    infoVC.contentString = model.content;
    
    HIDDEN_TAB;
    
    [self.navigationController pushViewController:infoVC animated:YES];
    
    SHOW_TAB;
}


- (void)pushToInfoVC:(NSNotification *)noti{
    
    NSString * code = noti.userInfo[@"button"];
    
    if ([code isEqualToString:@"one"]) {
        
        ReadModel * model = self.readListArray[2];
        
        ReadInfoViewController * infoVC = [ReadInfoViewController new];
        
        NSURL * url = [NSURL URLWithString:model.images];
        
        infoVC.url = url;
        infoVC.titleString = model.title;
        infoVC.contentString = model.content;
        
        HIDDEN_TAB;
        
        [self.navigationController pushViewController:infoVC animated:YES];
        
        SHOW_TAB;
    }else if ([code isEqualToString:@"two"]){
        
        ReadModel * model = self.readListArray[3];
        
        ReadInfoViewController * infoVC = [ReadInfoViewController new];
        
        NSURL * url = [NSURL URLWithString:model.images];
        
        infoVC.url = url;
        infoVC.titleString = model.title;
        infoVC.contentString = model.content;
        
        HIDDEN_TAB;
        
        [self.navigationController pushViewController:infoVC animated:YES];
        
        SHOW_TAB;
    }else if ([code isEqualToString:@"three"]){
        
        ReadModel * model = self.readListArray[1];
        
        ReadInfoViewController * infoVC = [ReadInfoViewController new];
        
        NSURL * url = [NSURL URLWithString:model.images];
        
        infoVC.url = url;
        infoVC.titleString = model.title;
        infoVC.contentString = model.content;
        
        HIDDEN_TAB;
        
        [self.navigationController pushViewController:infoVC animated:YES];
        
        SHOW_TAB;
    }else if ([code isEqualToString:@"four"]){
        
        ReadModel * model = self.readListArray[4];
        
        ReadInfoViewController * infoVC = [ReadInfoViewController new];
        
        NSURL * url = [NSURL URLWithString:model.images];
        
        infoVC.url = url;
        infoVC.titleString = model.title;
        infoVC.contentString = model.content;
        
        HIDDEN_TAB;
        
        [self.navigationController pushViewController:infoVC animated:YES];
        
        SHOW_TAB;
    }
}


- (void)getReadData:(NSNotification *)noti{
    
    NSString * type = noti.userInfo[@"type"];
    
    [ReadModel readWithType:type WithSucess:^(NSArray<ReadModel *> *array) {
        
        self.readListArray = array;
    } error:^{
        
        NSLog(@"productList error");
    }];
}

- (void)pushToSearchVC{
    
    HIDDEN_TAB;
    
    SearchViewController * searchVC = [SearchViewController new];
    
    [self.navigationController pushViewController:searchVC animated:YES];
    
    SHOW_TAB;
}


//- (void)goContentAction{
//
//    [[ZSContentNetworkTools sharedTools]getContentResultWithBlock:^(BOOL success, id responseData) {
//        if(success)
//        {
//            BOOL isCheck = [responseData[@"is_check"] boolValue];
//
//            if(isCheck){
//                ZSContentViewController *contentVc = [[ZSContentViewController alloc]init];
//                contentVc.urlStr = responseData[@"check_url"];
//
//                AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//
//                appDelegate.window.rootViewController = contentVc;
//            }
//
//            NSLog(@"%@",responseData);
//
//        }
//
//
//    }];
//
//}


- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
